const TOKEN_KEY = 'token'

export function setToken(token) {
  localStorage.setItem(TOKEN_KEY, token)
}

export function getToken() {
  return localStorage.getItem(TOKEN_KEY)
}

export function removeToken() {
  localStorage.removeItem(TOKEN_KEY)
}

export function parseJwt(token = getToken()) {
  try {
    const base64Url = token.split('.')[1]
    const base64 = base64Url.replace(/-/g, '_').replace(/_/g, '/')
    return JSON.parse(window.atob(base64))
  } catch (e) {
    return null
  }
}
